package com.mm.service.impl;

import com.mm.config.CustomProperties;
import com.mm.dao.PictureMapper;
import com.mm.exception.ServiceException;
import com.mm.model.Picture;
import com.mm.service.PictureService;
import com.mm.util.UUIDUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 * @date 2018/7/19 16:01
 */
@Service(value = "pictureService")
public class PictureServiceImpl implements PictureService {
	private static Logger logger = LoggerFactory.getLogger(PictureServiceImpl.class);

	@Autowired
	private PictureMapper pictureMapper;
	@Autowired
	private CustomProperties customProperties;

	/**
	 * 上传图片
	 *
	 * @param pictureFile MultipartFile
	 */
	@Override
	public Map<String, Object> uploadePicture(MultipartFile pictureFile) throws ServiceException {
		if (pictureFile == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//获取文件保存目录
		String pictureSaveDir = customProperties.getPictureSaveDir();
		if (StringUtils.isBlank(pictureSaveDir)) {
			throw new ServiceException("图片保存目录为空");
		}
		//获取图片原始名
		String originalFilename = pictureFile.getOriginalFilename();
		//图片后缀
		String extension = FilenameUtils.getExtension(originalFilename);
		//没有后缀的图片名
		String picutreName = originalFilename.replaceAll("." + extension, "");
		//图片保存绝对路径
		String fullPath = pictureSaveDir + originalFilename;
		//写入磁盘
		File file = new File(fullPath);
		//自动关闭流
		try (FileOutputStream fos = new FileOutputStream(file)) {
			file.createNewFile();
			fos.write(pictureFile.getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error("PictureServiceImpl uploadePicture 文件不存在");
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("PictureServiceImpl uploadePicture IO异常");
		}
		//将图片信息保存到图片表
		Picture picture = new Picture();
		//生产成一个UUID图片名称
		picture.setPictureName(UUIDUtils.getUUIDString());
		picture.setOriginalPictureName(originalFilename);
		picture.setFullPath(fullPath);
		picture.setPictureSuffix(extension);
		picture.setSaveDir(pictureSaveDir);
		picture.setAddTime(System.currentTimeMillis());
		savePicture(picture);
		//返回图片相关信息
		Map<String, Object> msgMap = new HashMap<>();
		msgMap.put("pictureId", picture.getPictureId());
		return msgMap;
	}

	/**
	 * 保存图片
	 *
	 * @param picture Picture
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void savePicture(Picture picture) throws ServiceException {
		if (picture == null) {
			throw new ServiceException("输入参数不能为空");
		}
		pictureMapper.insertPicture(picture);
	}

	/**
	 * 更新图片
	 *
	 * @param picture Picture
	 */
	@Override
	public void updatePicture(Picture picture) {
		pictureMapper.updatePictureById(picture);
	}

	/**
	 * 获取图片
	 *
	 * @param pictureId 图片ID
	 * @return Picture
	 * @throws ServiceException
	 */
	@Override
	public Picture getOnePictureById(Long pictureId) throws ServiceException {
		if (pictureId == null) {
			throw new ServiceException("图片ID不能为空");
		}
		return pictureMapper.getOnePictureById(pictureId);
	}

	/**
	 * 通过图片ID删除图片
	 *
	 * @param pictureId 图片ID
	 * @throws ServiceException
	 */
	@Override
	public void deletePictureById(Long pictureId) throws ServiceException {
		if (pictureId == null) {
			throw new ServiceException("图片ID不能为空");
		}
		pictureMapper.deletePictureById(pictureId);
	}

	/**
	 * 获取个人简历关联的图片列表
	 *
	 * @param resumeId 个人简历ID
	 * @return List<Long>
	 */
	@Override
	public List<Long> listPictureIdByResumeId(Long resumeId) throws ServiceException {
		if (resumeId == null) {
			throw new ServiceException("个人简历ID不能为空");
		}
		return pictureMapper.listPictureIdByResumeId(resumeId);
	}
}
